<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link href="/css/bootstrap.css" rel="stylesheet">
    <link href="/css/layui.css" rel="stylesheet">
</head>

<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
<script type="text/javascript" src="/js/jquery.js"></script>
<script src="/js/popper.min.js"></script>
<script type="text/javascript" src="/js/bootstrapValidator.min.js"></script>
<!-- 包括所有已编译的插件 -->
<script type="text/javascript" src="/js/bootstrap.js"></script>
<script type="text/javascript" src="/js/layui.js"></script>

</head>
<body background="image/2.jpg"
      style="background-repeat:no-repeat;
               background-attachment:fixed;
               background-size:100% 100%; " >
<h1 style="text-align: center;margin-top: 50px">欢迎您访问数据组文章采集页面</h1>
<div class="form-container">
    <form class="form-horizontal needs-validation" id="forminfo"
          method="post" role="form" novalidate>
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">文章标题</label>
            <div class="col-sm-10">
                <input type="text" class="form-control form-checks" id="title" name="title"
                       placeholder="请输入文章标题" required>
            </div>
        </div>
        <div class="form-group">
            <label for="url" class="col-sm-2 control-label">文章地址</label>
            <div class="col-sm-10">
                <input type="url" class="form-control form-checks" id="url" name="url"
                       placeholder="请输入文章地址" required>
            </div>
        </div>
        <div class="form-group">
            <label for="submit_user" class="col-sm-2 control-label">姓名</label>
            <div class="col-sm-10">
                <input type="text" class="form-control form-checks" id="submit_user" name="submit_user"
                       placeholder="姓名" required>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" id="submit_btn" class="btn btn-success">提交</button>
            </div>
        </div>
    </form>
<!--    <div class="alert alert-success alert-dismissible hide">-->
<!--        <button type="button" class="close" data-dismiss="alert">&times;</button>-->
<!--        <strong>数据保存成功</strong>-->
<!--    </div>-->

</div>
</body>
<script>
    $(function () {
        $("form").bootstrapValidator({
            // 表单框里右侧的icon
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            submitHandler: function (validator, form, submitButton) {
                // 表单提交成功时会调用此方法
                // validator
                // validator: 表单验证实例对象
                // form  jq对象  指定表单对象
                // submitButton  jq对象  指定提交按钮的对象
                // submitButton
            },
            fields: {
                title: {
                    message: '文章标题不能为空',
                    validators: {
                        notEmpty: {
                            message: '文章标题不能为空'
                        },
                        remote: { // ajax校验，获得一个json数据（{'valid': true or false}）
                            url: '/newsinfo/validator',       //验证地址
                            message: '文章标题已存在',   //提示信息
                            dataType:'json',
                            type: 'POST',          //请求方式
                            data: function(validator){  //自定义提交数据，默认为当前input name值
                               titleVal =  $("input[name='title']").val()
                                return {title:titleVal}
                            }
                        }
                    }
                },
                url: {
                    message: '文章地址不符合要求',
                    validators: {
                        notEmpty: {
                            message: '文章地址不能为空'
                        },
                        url: {
                            message: "文章地址不符合要求"
                        },
                        remote: { // ajax校验，获得一个json数据（{'valid': true or false}）
                            url: '/newsinfo/validator',       //验证地址
                            message: '文章地址已存在',   //提示信息
                            dataType:'json',
                            type: 'POST',          //请求方式
                            data: function(validator){  //自定义提交数据，默认为当前input name值
                                urlVal =  $("input[name='url']").val()
                                return {url:urlVal}
                            }
                        }
                    }
                },
                submit_user: {
                    message: '提交人不能为空',
                    validators: {
                        notEmpty: {
                            message: '提交人不能为空'
                        },
                    }
                }
            }
        })
        $("#submit_btn").click(function () {  //非submit按钮点击后进行验证，如果是submit则无需此句直接验证
            $("form").bootstrapValidator('validate');  //提交验证
            if ($("form").data('bootstrapValidator').isValid()) {  //获取验证结果，如果成功，执行下面代码
                var title = $("#title").val()
                var url = $("#url").val()
                var submit_user = $("#submit_user").val()
                let data = {"title": title, "url": url, "submitUser": submit_user}
                $.ajax({
                    type: "post",
                    url: "/newsinfo/submit",
                    data: JSON.stringify(data),
                    contentType: 'application/json',
                    success: function (res) {
                        if (res["code"] == 200) {
                            layui.use('layer', function(){
                                var layer = layui.layer;
                                layer.msg("提交成功",{icon:1});
                            });
                            setTimeout(function(){
                                location.reload();
                            }, 1000);
                            $('#forminfo').data('bootstrapValidator', null);
                        } else {
                            alert(res["message"])
                        }
                    },
                    processData: false,
                    contentType: false,
                    error: function (xhr) {
                        console.log(xhr)
                    }
                })
            }
        });

    });
</script>
<style type="text/css">
    body{
        /*background-image: ;*/
    }
    .form-container {
        text-align: center;
        background-color: rgba(0,0,0,0);
        border-radius: 20px;
        width: 500px;
        height: 350px;
        margin: auto;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
</style>
</html>